package com.yksj.healthtalk.ui.dictionary;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.Intent;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonHttpResponseHandler;
import com.yksj.healthtalk.ui.R;

/**
 * 
 * 医书第一级菜单
 * @author zhao
 *
 */
public class DictionaryFirstLevelActivity extends BaseFragmentActivity implements OnClickListener {
	ListView listView;
	DictionaryAdapter adapter;
	String levelId;
	String titleName;
	
	final JsonHttpResponseHandler responseHandler = new JsonHttpResponseHandler(this){
		public void onSuccess(int statusCode, org.json.JSONObject response) {
			try {
				JSONArray array = response.getJSONArray("data");
				List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
				for (int i = 0; i < array.length(); i++) {
					JSONObject object = array.getJSONObject(i);
					String id = object.getString("CODE");
					String name = object.getString("NAME");
					
					HashMap<String,String> map = new HashMap<String, String>();
					map.put("NAME",name);
					map.put("ID",id);
					list.add(map);
				}
				if(!isFinishing()){
					listView.setAdapter(adapter = new DictionaryAdapter(DictionaryFirstLevelActivity.this,list));
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.dictionary_first_level_layout);
		
		levelId = getIntent().getStringExtra("parame");
		titleName = getIntent().getStringExtra("title");
		
		initView();
		
		initData();
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
	
	private void initData(){
		if("3".equals(levelId)){
			HttpRestClient.doHttpQueryInspectionsServlet(null,responseHandler);
		}else{
			listView.setAdapter(adapter = new DictionaryAdapter(this,parseXmlData()));
		}
	}
	
	private List<HashMap<String,String>> parseXmlData(){
		final List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
		XmlResourceParser parser = getResources().getXml(R.xml.dictionary);
		try {
			int level = 0;//1开始到解析位置,2解析完毕
			//文件是否到末尾了
			while(parser.getEventType() != XmlResourceParser.END_DOCUMENT){
				if(parser.getEventType() == XmlResourceParser.START_TAG){
					String tagName = parser.getName();
					if("menu".equals(tagName)){
						String id = parser.getAttributeValue(0);
						if(levelId.equals(id)){//判断菜单级id是否相等
							level = 1;
						}else{
							parser.nextTag();
							continue;
						}
					}else if("item".equals(tagName)){
						if(level == 1){
							HashMap<String,String> map = new HashMap<String, String>();
							String name = parser.getAttributeValue(0);
							String id = parser.getAttributeValue(1);
							map.put("NAME",name);
							map.put("ID",id);
							list.add(map);
						}
					}
				}else if(parser.getEventType() == XmlResourceParser.END_TAG){
					String tagName = parser.getName();
					if(level == 1 && "menu".equals(tagName)){
						break;
					}
				}
				parser.next();
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			parser.close();
		}
		return list;
	}
	
	private void initView(){
		initTitle();
//		titleLeftBtn.setBackgroundResource(R.drawable.doctorbook_back);
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText(titleName);
		
		listView = (ListView)findViewById(android.R.id.list);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				HashMap<String, String>  map = adapter.getItem(position);
				String idStr = map.get("ID");
				String name = map.get("NAME");
				view.setSelected(true);
				onActivityStart(idStr,name);
			}
		});
	}
	
	private void onActivityStart(String code,String title){ 
		Intent intent = null;
		if("3".equals(levelId)){
			intent  = new Intent(this,DictionaryCheckActivity.class);
		}else{
			intent = new Intent(this,DictionarySecendActivity.class);
		}
		intent.putExtra("parame",code);
		intent.putExtra("title",title);
		startActivity(intent);
	}
	
	class DictionaryAdapter extends BaseAdapter{
		final LayoutInflater layoutInflater;
		final List<HashMap<String,String>> listMaps = new ArrayList<HashMap<String,String>>();
		
		public DictionaryAdapter(Context context,List<HashMap<String,String>> list) {
			layoutInflater = LayoutInflater.from(context);
			listMaps.addAll(list);
		}
		
		@Override
		public int getCount() {
			return listMaps.size();
		}

		@Override
		public HashMap<String,String> getItem(int position) {
			return listMaps.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			HashMap<String,String> hashMap = getItem(position);
			ViewHolder holder;
			
			if(convertView == null){
				holder  = new ViewHolder();
				convertView = layoutInflater.inflate(R.layout.dictionary_level1_list_item,null);
				convertView.setTag(holder);
				holder.textView = (TextView)convertView.findViewById(R.id.nameTxt);
			}else{
				holder = (ViewHolder)convertView.getTag();
			}
			String name = hashMap.get("NAME");
			holder.textView.setText(name);
			return convertView;
		}
	}
	
	private static class ViewHolder{
		TextView textView;
	}
	
	
}
